import {
	CalendarViewActionType,
	ICalendarViewAbility,
	IParam,
} from '@/core/interface';
import { ICalendarViewController } from '@/core/interface/view/controller';
import { ICalendarViewControllerParams } from '@/core/interface/view/controller-params';
import { ICalendarViewStore } from '@/core/interface/view/store';
import { ICalendarAbility } from '@/core/interface/widgets/ability';
import { deepCopy, getNowMonthFirst, getNowMonthLast } from '@/core/utils';
import { MDViewControllerBase } from './md-view-controller-base';

/**
 * 日历视图控制器
 *
 * @export
 * @class CalendarViewController
 * @extends {ViewController}
 */
export class CalendarViewController
	extends MDViewControllerBase<
		CalendarViewActionType,
		ICalendarViewStore,
		ICalendarViewAbility
	>
	implements ICalendarViewController<ICalendarViewStore, ICalendarViewAbility>
{
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {ICalendarViewControllerParams<CalendarViewActionType, ICalendarViewAbility>} params
	 * @memberof CalendarViewController
	 */
	protected processViewInit(
		params: ICalendarViewControllerParams<
			CalendarViewActionType,
			ICalendarViewAbility
		>
	) {
		super.processViewInit(params);
	}

	/**
	 * 获取日历部件能力
	 *
	 * @protected
	 * @return {*}  {(ICalendarAbility | undefined)}
	 * @memberof CalendarViewController
	 */
	protected getMDCtrlAbility(): ICalendarAbility | undefined {
		const calendar = this.model.ctrls.find(
			(ctrl: IParam) => ctrl.controlType === 'CALENDAR'
		);
		if (calendar) {
			return this.getSubAbility<ICalendarAbility>(calendar.name);
		}
		return undefined;
	}

	/**
	 * 视图加载
	 *
	 * @param {IParam} [opts={}]
	 * @return {*}
	 * @memberof MDViewControllerBase
	 */
	public viewMounted(opts: IParam = {}) {
		this.initViewMsg();
		this.initCounterData();
		if (this.model.useDefaultLayout) {
			if (!this.isLoadDefault) {
				this.isLoadDefault = true;
				return;
			}
			if (
				this.model.enableQuickGroup &&
				this.model.quickGroupCodeList &&
				!this.isEmitQuickGroupValue
			) {
				return;
			}
			const searchForm = this.getSearchFormAbility();
			if (searchForm) {
				const tempViewParams = deepCopy(this.store.viewParams);
				searchForm.loadDraft(tempViewParams);
				return;
			}

			this.load({ arg: { start: getNowMonthFirst(), end: getNowMonthLast() } });
		} else {
			this.initLayout().then(() => {
				this.load();
			});
		}
	}
}
